package com.ruoyi.web.controller.system;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.system.domain.SysSqlLog;
import com.ruoyi.system.service.ISysSqlLogService;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.framework.annotation.RyLog;

import java.util.Arrays;
import java.util.List;

/**
 * SQL审计 操作处理
 * @author ruoyi
 */
@Controller
@Mapping("system/sqlLog")
public class SysSqlLogController extends BaseController {

    @Inject
    ISysSqlLogService sysSqlLogService;

    /**
     * 查询SQL审计列表
     */
    @AuthPermissions("system:sqlLog:list")
    @Get
    @Mapping("list")
    public PageData<SysSqlLog> list(Page<SysSqlLog> page, SysSqlLog sysSqlLog) {
        QueryWrapper qw = getQW(sysSqlLog);
        Page<SysSqlLog> result = sysSqlLogService.page(page, qw);
        return getPageData(result);
    }

    /**
     * 导出SQL审计列表
     */
    @RyLog(title = "SQL审计", businessType = BusinessType.EXPORT)
    @AuthPermissions("system:sqlLog:export")
    @Post
    @Mapping("export")
    public void export(SysSqlLog sysSqlLog) {
        QueryWrapper qw = getQW(sysSqlLog);
        List<SysSqlLog> list = sysSqlLogService.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 删除SQL审计
     */
    @RyLog(title = "SQL审计", businessType = BusinessType.DELETE)
    @AuthPermissions("system:sqlLog:remove")
    @Delete
    @Mapping("{sqlIds}")
    public Result<Void> delete(@Path Long[] sqlIds) {
        return toResult(sysSqlLogService.removeByIds(Arrays.asList(sqlIds)));
    }

    /**
     * 清空SQL审计
     */
    @RyLog(title = "SQL审计", businessType = BusinessType.CLEAN)
    @AuthPermissions("system:sqlLog:remove")
    @Delete
    @Mapping("clean")
    public Result<Void> clean() {
        sysSqlLogService.clean();
        return Result.succeed();
    }

    private QueryWrapper getQW(SysSqlLog sysSqlLog) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(SysSqlLog::getPlatform).eq(sysSqlLog.getPlatform());
        qw.and(SysSqlLog::getModule).eq(sysSqlLog.getModule());
        qw.and(SysSqlLog::getUrl).eq(sysSqlLog.getUrl());
        qw.and(SysSqlLog::getUser).eq(sysSqlLog.getUser());
        qw.and(SysSqlLog::getUserIp).eq(sysSqlLog.getUserIp());
        qw.and(SysSqlLog::getHostIp).eq(sysSqlLog.getHostIp());
        qw.and(SysSqlLog::getQuery).eq(sysSqlLog.getQuery());
        qw.and(SysSqlLog::getQueryParams).eq(sysSqlLog.getQueryParams());
        qw.and(SysSqlLog::getQueryTime).eq(sysSqlLog.getQueryTime());
        qw.and(SysSqlLog::getElapsedTime).ge(sysSqlLog.getElapsedTime());
        qw.and(SysSqlLog::getDsName).like(sysSqlLog.getDsName());
        qw.and(SysSqlLog::getQueryTime).ge(sysSqlLog.getStartTime());
        qw.and(SysSqlLog::getQueryTime).le(sysSqlLog.getEndTime());
        return qw;
    }

}
